package com.ia.aistream.service.operating.impl;

import com.ia.aistream.common.utils.spring.SpringUtils;
import com.ia.aistream.manager.common.ICommonFileManager;
import com.ia.aistream.manager.dxfcomponent.conf.DrawConf;
import com.ia.aistream.manager.dxfcomponent.util.DrawExtensionUtil;
import com.ia.aistream.manager.framework.redis.KeyFiled;
import com.ia.aistream.manager.framework.util.ShiroUtils;
import com.ia.aistream.manager.operating.conf.DocMapConf;
import com.ia.aistream.manager.operating.util.DocFileData;
import com.ia.aistream.mapper.operating.*;
import com.ia.aistream.model.common.entity.CommonFile;
import com.ia.aistream.model.dxfcomponent.entity.DxfDraw;
import com.ia.aistream.model.operating.entity.*;
import com.ia.aistream.service.operating.IDocContentPargraphAndTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class DocContentPargraphAndTitleServiceImpl implements IDocContentPargraphAndTitleService {

    @Autowired
    private DocContentParagraphMapper docContentParagraphMapper;
    @Autowired
    private DocRevMapper docRevMapper;
    @Autowired
    private DocAppendixMapper docAppendixMapper;
    @Autowired
    private DocContentTableMapper docContentTableMapper;
    @Autowired
    private DocApprovalMapper docApprovalMapper;
    @Autowired
    private DocAppendixParagraphMapper docAppendixParagraphMapper;
    @Autowired
    private ICommonFileManager iCommonFileService;
    @Autowired
    private DrawConf drawConf;

    /**
     * 读取doc文档，存入数据库中
     * @param fileName
     * @return
     */
    @Override
    @Transactional
    public Integer saveDocDateSource(MultipartFile[] fileName) throws Exception {
        // 遍历上传的文件集
        for(MultipartFile file : fileName){
            // 实现文件上传
            CommonFile commonFile = iCommonFileService.wordUpload(file);
            // 获取上传的文件后缀
            String fileType = commonFile.getFileExt();
            String type = "/" + fileType;
            // 获取服务器上的文件地址
            String filePath = drawConf.getFileDir() + commonFile.getDownloadUrl().substring(commonFile.getDownloadUrl().indexOf(type));
            // 解析文档数据
            Map<String, Object> map = DocFileData.getDocFileData(filePath, fileType);

            // 获取文档审批记录对象
            DocApproval docApproval = (DocApproval) map.get(DocMapConf.DOC_INDEX_APPROVAL.value);
            docApprovalMapper.insertDocApproval(docApproval);

            // 获取文档版本修改记录
            List<DocRev> docRevList  = (List<DocRev>) map.get(DocMapConf.DOC_REV_LIST.value);
            if(docRevList!=null){
                docRevMapper.insertDocRevList(docRevList);
            }


//             获取段落内容
            List<DocContentParagraph> list = (List<DocContentParagraph>) map.get(DocMapConf.DOC_PARAGRAPH_List.value);
            if(list!=null && list.size()>0){
                docContentParagraphMapper.inserDocContentParagraphList(list);
            }

            // 获取表格内容数据
            List<DocContentTable> tableList = (List<DocContentTable>) map.get(DocMapConf.DOC_TABLE_LIST.value);
            if(tableList!=null && tableList.size()>0){
                docContentTableMapper.inserDocContentTableList(tableList);
            }

            // 获取附录表格内容
            List<DocAppendix>  appendixList= (List<DocAppendix>) map.get(DocMapConf.DOC_APPENDIX.value);
            if(appendixList!=null && appendixList.size()>0){
                docAppendixMapper.insertDocAppendixList(appendixList);
            }
            // 获取附录段落内容
            List<DocAppendixParagraph> docAppendixParagraphList = (List<DocAppendixParagraph>) map.get(DocMapConf.DOC_APPENDIX_PAR_LiST.value);
            if(docAppendixParagraphList!=null && docAppendixParagraphList.size()>0){
                docAppendixParagraphMapper.insertDocAppendixParagraphList(docAppendixParagraphList);
            }
        }
        return 1;
    }
}
